/**
 * @file       asin_02.c
 * @brief      asin
 *
 * @since      2003-07-17
 * @author     D. Andoleit
 *
 * Copyright (c) 2003 by dSPACE GmbH, Paderborn, Germany
 * All Rights Reserved
 *
 */

/*
 * $Workfile: ASIN_02.c $
 * $Revision: 1.1 $
 * $Date: 2004/04/28 11:44:53GMT+01:00 $
 */


#include "dsfxp.h"

/******************************************************************************
*
* FUNCTION:
*   F__I16ASINI16(v)
*
* DESCRIPTION:
*   Calculates arcsin of 16 bit input using non equidistant lookup table.
*
* PARAMETERS:
*   Int32   v      input value
*
* RETURNS:
*   Int32   fcn    return value
*
* NOTE:
*
*
******************************************************************************/
static const UInt16 y_table[128] =
{
0,1279,2079,2743,3334,3875,4379,4855,5307,5740,6155,6556,6943,7319,7685,8039,
8384,8723,9051,9373,9689,9998,10300,10598,10890,11177,11459,11735,12007,12274,
12537,12796,13053,13303,13550,13794,14034,14272,14505,14734,14960,15184,15404,
15622,15834,16046,16254,16459,16662,16862,17058,17254,17446,17636,17823,18006,
18188,18366,18542,18716,18889,19058,19227,19392,19553,19715,19870,20026,20180,
20329,20479,20624,20769,20912,21052,21189,21326,21459,21589,21719,21844,21970,
22094,22214,22334,22447,22564,22676,22785,22893,22999,23104,23206,23305,23402,
23502,23593,23687,23777,23862,23952,24037,24116,24189,24276,24345,24418,24495,
24563,24635,24696,24761,24813,24887,24947,24990,25035,25109,25136,25193,25224,
25293,25331,25374,25422,25480,25555,25736
};
static const UInt16 x_table[128] =
{
0,1278,2073,2730,3311,3839,4327,4784,5215,5623,6011,6382,6737,7078,7406,7720,
8023,8317,8598,8870,9134,9389,9635,9874,10106,10330,10547,10757,10961,11158,
11349,11534,11715,11889,12057,12221,12380,12534,12683,12827,12966,13102,13233,
13360,13482,13601,13716,13827,13935,14039,14139,14237,14331,14422,14510,14594,
14676,14754,14830,14903,14974,15042,15108,15171,15231,15290,15345,15399,15451,
15500,15548,15593,15637,15679,15719,15757,15794,15829,15862,15894,15924,15953,
15981,16007,16032,16055,16078,16099,16119,16138,16156,16173,16189,16204,16218,
16232,16244,16256,16267,16277,16287,16296,16304,16311,16319,16325,16331,16337,
16342,16347,16351,16355,16358,16362,16365,16367,16369,16372,16373,16375,16376,
16378,16379,16380,16381,16382,16383,16384
};

Int16 F__I16ASINI16(Int16 x_In)
{
    UInt16 y_Out;
    Int16 i = 0;
    UInt16 xi;
    Int8 sign = 1;


    if (x_In <= -16384) return -25736;
    if (x_In >= 16384) return 25736;

    if ( x_In < 0 )
    {
        x_In = (Int16)-x_In;
        sign = -1;
     }

    while (x_In > x_table[i+1])
    {
       i++;
    }

    xi = x_table[i];
    xi = (UInt16)((UInt32)((UInt32)(x_In - xi) << 14) / (UInt16)(x_table[i+1] - xi));

    y_Out = y_table[i];
    y_Out += (UInt16)((UInt32)((UInt32)(y_table[i+1] - y_Out) * xi) >> 14);

    return (Int16)(y_Out * sign);
}
/* END F__I16ASINI16() */
